package com.ggh.config;
import com.ggh.vo.Body;
import com.ggh.vo.Meta;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * @Author 18011618
 * @Description 全局异常拦截器
 * @Date 16:38 2018/7/5
 * @Modify By
 */
@Slf4j
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    private static final String logExceptionFormat = "Capture Exception By GlobalExceptionHandler: Code: %s Detail: %s";

    //运行时异常
    @ExceptionHandler(RuntimeException.class)
    public Body runtimeExceptionHandler(RuntimeException ex) {
        return Body.newInstance(Meta.CODE_500, ex);
    }

    //空指针异常
    @ExceptionHandler(NullPointerException.class)
    public Body nullPointerExceptionHandler(NullPointerException ex) {
        System.err.println("NullPointerException:");
        return Body.newInstance(Meta.CODE_500, ex);
    }

    //类型转换异常
    @ExceptionHandler(ClassCastException.class)
    public Body classCastExceptionHandler(ClassCastException ex) {
        return Body.newInstance(Meta.CODE_500, ex);
    }

    //IO异常
    @ExceptionHandler(IOException.class)
    public Body iOExceptionHandler(IOException ex) {
        return Body.newInstance(Meta.CODE_500, ex);
    }

    //未知方法异常
    @ExceptionHandler(NoSuchMethodException.class)
    public Body noSuchMethodExceptionHandler(NoSuchMethodException ex) {
        return Body.newInstance(Meta.CODE_500, ex);
    }

    //数组越界异常
    @ExceptionHandler(IndexOutOfBoundsException.class)
    public Body indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException ex) {
        return Body.newInstance(Meta.CODE_500, ex);
    }

    //400错误
    @ExceptionHandler({HttpMessageNotReadableException.class})
    public Body requestNotReadable(HttpMessageNotReadableException ex) {
        System.out.println("400..requestNotReadable");
        return   Body.newInstance(Meta.CODE_400, ex);
    }

    //400错误
    @ExceptionHandler({TypeMismatchException.class})
    public Body requestTypeMismatch(TypeMismatchException ex) {
        System.out.println("400..TypeMismatchException");
        return Body.newInstance(Meta.CODE_500, ex);
    }

    //400错误
    @ExceptionHandler({MissingServletRequestParameterException.class})
    public Body requestMissingServletRequest(MissingServletRequestParameterException ex) {
        System.out.println("400..MissingServletRequest");
        return Body.newInstance(Meta.CODE_400, ex);
    }

    //405错误
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    public Body request405(HttpRequestMethodNotSupportedException ex) {
        return Body.newInstance(Meta.CODE_460, ex);
    }

    //406错误
    @ExceptionHandler({HttpMediaTypeNotAcceptableException.class})
    public Body request406(HttpMediaTypeNotAcceptableException ex) {
        System.out.println("406...");
        return Body.newInstance(Meta.CODE_500, ex);
    }

    //500错误
    @ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class})
    public Body server500(RuntimeException ex) {
        System.out.println("500...");
        return Body.newInstance(Meta.CODE_500,ex);
    }

    //栈溢出
    @ExceptionHandler({StackOverflowError.class})
    public Body requestStackOverflow(StackOverflowError ex) {
        return Body.newInstance(Meta.CODE_500,ex);
    }

    //除数不能为0
    @ExceptionHandler({ArithmeticException.class})
    public Body arithmeticException(ArithmeticException ex) {
        return Body.newInstance(Meta.CODE_500,ex);
    }


    //其他错误
    @ExceptionHandler({Exception.class})
    public Body exception(Exception ex) {
        return Body.newInstance(Meta.CODE_500,ex);
    }


}